<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 卡片的内容区：放表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="380">
        </el-table-column>
        <el-table-column prop="creator" label="作者" width="200">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数" width="180">
        </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200">
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格结束，放分页组件 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 抽屉组件 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      :before-close="handleClose"
      size="50%"
    >
      <!-- 抽屉的内容区 start -->
      <el-form label-width="80px" :model="form" :rules="rules" ref="form">
        <!-- 第1行 -->
        <el-form-item label="标题" prop="stem">
          <el-input placeholder="请输入标题" v-model="form.stem"></el-input>
        </el-form-item>
        <!-- 第2行 -->
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <!-- 第3行 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 抽屉的结尾 edd -->
    </el-drawer>
  </div>
</template>

<script>
// 按需导入，所需要的API方法
import {
  articleListAPI,
  createArticleAPI,
  removeArticleAPI
} from '@/api/article'

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      current: 1, // 页码，默认获取第1页的数据
      pageSize: 10, // 每页新书多少条数据
      list: [], // 准备存储接口返回的数据
      total: 0, // 数据总数，一共有多少条数据
      drawer: false, // true表示打开抽屉；false表示关闭抽屉
      drawerType: '', // 表示操作类型 (add|edit|view)
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    // 分页获取列表数据
    async initData () {
      const { data } = await articleListAPI({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(data);
      this.list = data.rows
      this.total = data.total
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(async () => {
          // 点击了确定
          // console.log(id)
          await removeArticleAPI(id)
          this.$message.success('删除成功')
          this.initData()
        })
        .catch(() => {
          // 点击了取消
        })
    },
    // 修改每页多少条之后，触发这个方法
    handleSizeChange (val) {
      // console.log(val) // 参数表示修改后的值
      this.pageSize = val // 比如修改每页显示15条数据
      this.current = 1
      this.initData()
    },
    // 页码切换后，触发的方法
    handleCurrentChange (val) {
      // console.log(val) // 表示切换后的页码值
      this.current = val // 比如点击3，修改数据项为3
      this.initData() // 重新发送请求，获取第3页的数据
    },
    // 关闭抽屉之前执行的方法
    handleClose () {
      this.$refs.form.resetFields() // 清空表单
      // console.log(123)
      this.drawer = false // 关闭抽屉
    },
    // 打开抽屉的方法
    openDrawer (type, id) {
      this.drawerType = type
      // console.log(type, id) // add undefined | edit 40846 | view 40846
      this.drawer = true // 修改数据项，打开抽屉
    },
    // 表单提交的方法
    onSubmit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          await createArticleAPI(this.form)
          this.$message.success('登陆成功')
          this.current = 1
          this.initData()
          this.handleClose()
        }
      })
    }
  },
  computed: {
    drawerTitle () {
      if (this.drawerType === 'add') {
        return '添加面经'
      } else if (this.drawerType === 'edit') {
        return '编辑面经'
      } else {
        return '面经预览'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
